 //index.js
//获取应用实例
var api = require('../../utils/api.js');
var rsa = require('../../utils/rsa.js')
var rsocket = require('../../utils/rsocket.js')
const app = getApp()

Page({
  data: {
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    contentImage: [{ tittle: '收款构成', image: "/pages/images/shouk.png" }, { tittle: '销售日报', image: "/pages/images/ribao.png" }, { tittle: '销售排行', image: "/pages/images/xiaoshou.png" }, { tittle: '交易查询', image: "/pages/images/jycx.png" }, { tittle: '库存管理', image: "/pages/images/kucun.png" }, { tittle: '会员', image: "/pages/images/keliul.png" }],

    ramount: '--', //今日应收
    namount: '--', //实收 
    grate: '--', //毛利
    prate: '--', //毛利率
    telphone:'',    
    shopname:'',
    msgList: [],

    imgUrls: [],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    swiperCurrent: 0,
  },

  //轮播图的切换事件
  swiperChange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
   //轮播图点击事件
  onItemClick: function (event) {
    const index = this.data.swiperCurrent;
    var lis = this.data.imgUrls;
    var load_url = lis[index].url;
    if(load_url){
      wx.navigateTo({
        url: '/pages/webLoad/webLoad?webrl=' + load_url ,
      })
    }
   
  },
  onShow:function(){
    this.postgetGrossProfit();
  },
  reqestPic:function(){
    var that = this;
    wx.request({
      url: 'https://yklmt.etonepay.com/pos/picture/selectUrl', 
      success: function (res) {
        console.log(res.data);
        // var json = JSON.parse(res.data);
        if (res.data) {
          that.setData({
            imgUrls: res.data,
          })
        } else {

        }
      }
    })
  },

  onLoad: function (options) {
    this.reqestPic();
    const tet = options.telphone;
    var dst = tet.substring(0, 3) + "****" + tet.substring(7);
    var sh = app.globalData.shopName ;

    this.setData({
      telphone: dst,
      shopname:sh,
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true,
      })
    }else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    var openvoice = wx.getStorageSync('voice_open');
    if (openvoice){
      // rsocket.openWebSocket();
    }
  },
  //获取应收，实收，毛利率统计
  postgetGrossProfit: function () {
    const setion = app.globalData.session;
    if (!setion) return;
    const sebn = app.globalData.shoopnum;
    const datetemp = this.formatDate(new Date());
    wx.showLoading({
      title: '刷新中...',
      mask:true,
    })

    console.log('当前日期：' + datetemp);
    rsa.Cpic_fetchPost(rsa.SERVERPORT, { 'method': 'store.getprofit', "seller_bn": sebn, "session_id": setion, "store_id": '', "trade_date": datetemp}, (err, res) => {
      wx.stopPullDownRefresh()
      wx.hideLoading();
      // console.log("err:" + err + "DIC:" + res.ReturnMsg + 'GROUP：' + res.data);
      if (res.data) {
        const receivable_amount = res.data.receivable_amount ? res.data.receivable_amount:'0.00';  //应收 
        const net_receipts_amount = res.data.receivable_count ? res.data.receivable_count:'--'; //笔数
        const gross_profit_rate = res.data.gross_profit_rate ? res.data.gross_profit_rate:'0.00'; //毛利率 

        var reciver =parseFloat(receivable_amount).toFixed(2);
        var net_receipts = parseFloat(net_receipts_amount) .toFixed(2);
        var gross_profit = parseFloat(gross_profit_rate).toFixed(2);
        var pd = '';
        if (net_receipts == 0.00) {
          pd = '0.00';
        } else {
          pd = (parseInt(gross_profit_rate) / parseInt(net_receipts_amount) * 100).toFixed(2);
        }
        this.setData({
          ramount: reciver,
          namount: net_receipts_amount,
          grate: gross_profit,
          prate: pd,
        })
      }
    }) 
  },
  //支付核算
  payaction:function(){
    const value = app.globalData.userstatus;
    if(value == 0){
      wx.navigateTo({
        url: '/pages/userCenter/merIdentify',
      })
      return;
    }
    if(value == 1){
      wx.showModal({
        title: '提示',
        content: '资质审核中，请耐心等待',
        showCancel:false,
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/content/payment',
    })
  },

  //格式化日期：yyyy-MM-dd
  formatDate(date) {
    var myyear = date.getFullYear();
    var mymonth = date.getMonth() + 1;
    var myweekday = date.getDate();

    if (mymonth < 10) {
      mymonth = "0" + mymonth;
    }
    if (myweekday < 10) {
      myweekday = "0" + myweekday;
    }
    return (myyear + "-" + mymonth + "-" + myweekday);
  },

  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  jumplogin: function () {
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },
  nexshow: function (e) {
    console.log(e);
    var cindex = e.currentTarget.dataset.idx;
    const value = app.globalData.userstatus;
    if (value == 0) {
      wx.navigateTo({
        url: '/pages/userCenter/merIdentify',
      })
      return;
    }
     if(value == 1){
      wx.showModal({
        title: '提示',
        content: '审核中,请等待审核！',
        showCancel:false,
      })
      return;
    }
    switch (cindex) {
      case 0:
        wx.navigateTo({
          url: '/pages/content/paydetial',
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
        break;
      case 1:
        wx.navigateTo({
          url: '/pages/content/datesale',
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
        break;
      case 3:
        wx.navigateTo({
          url: '/pages/content/busiList',
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
        break;
      case 4:
        wx.navigateTo({
          url: '/pages/shopStore/main',
        })
        break;
      case 2:
        wx.navigateTo({
          url: '/pages/content/xspaih',
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
        break;
      case 5:
        wx.navigateTo({
          url: '/pages/content/vipuser',
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
        break;
    }
  },
  //设置
  settAction:function(){
    wx.navigateTo({
      url: '/pages/content/setting',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 下拉刷新  
  onPullDownRefresh: function () {
    this.postgetGrossProfit();
  },
})
